"""
    Controller for position table. Contains UCVT's staff positions.
"""

from mnemoply import model
from mnemoply.controllers.lookup import LookupController, TableCol, SQLObjectLookupDataSource
from mnemoply.controllers.crud import CRUDController, ItemActionsTab

class PersonsItemTab(ItemActionsTab):
    title = _("Person")
    name = "persons_tab"
    ma_class_name = 'Person'

    def getRecords(self, obj):
        personPositions = list(model.PersonPosition.selectBy(position=obj))
        from sets import Set
        persons = Set()
        for ppositions in personPositions:
            persons |= Set([ppositions.person])
        persons = list(persons)
        persons.sort(key=lambda x: x.title)
        return persons

    def getRecordTitle(self, obj):
        return obj.title

class PositionController(LookupController, CRUDController):
    model_object = model.Position
    item_tabs = [PersonsItemTab()]
    lookup_columns = [
        TableCol(name='name', caption=_('Name')),
        TableCol(name='comment', caption=_('Comment'), hidden=True),
    ]
    view_fields = ['name', 'comment']

    def __init__(self):
        LookupController.__init__(self,
            data_source=SQLObjectLookupDataSource(self.model_object),
            columns=self.lookup_columns,
            title=self.model_object.model_title,
        )
        CRUDController.__init__(self, model_object=self.model_object)

exposed = PositionController()
